
<?php 




//username
//portalname
//password
//mail

//create portal
//create user
//create page
//create content
//create authoring
//create presentation
//regiter content portelt
//edit preference
//send mail




//getParameter($portletUID."submit")==null || $error
if (true){
	?>

<form action="/" method="POST">
<?php setupForm(); ?>
<table>
<tr>
<td width="100px">
<label>Portal Name</label></td><td><?php formText($portletUID."portalname",20); ?></td>
</tr>

<tr height="5px">
<td colspan="2">

</td>
</tr>

<tr>
<td>
<label>Utilisateur</label></td><td><?php formText($portletUID."username",20); ?></td>
</tr>

<tr height="5px">
<td colspan="2">

</td>
</tr>

<tr>
<td>
<label>Mot de passe</label></td><td><?php formPassword($portletUID."password",20); ?></td>
</tr>

<tr height="5px">
<td colspan="2">

</td>
</tr>

<tr>
<td>
<label>Confirmation</label></td><td><?php formPassword($portletUID."confirmpassword",20); ?></td>
</tr>

<tr height="5px">
<td colspan="2">

</td>
</tr>

<tr>
<td>
<label>E-mail</label></td><td><?php formText($portletUID."mail",20); ?>
</td>
</tr>

<tr height="5px">
<td colspan="2">

</td>
</tr>

<tr>
<td>
<label>Langue</label></td><td>



<select  title="" name="<?php echo $portletUID; ?>language" id="<?php echo $portletUID; ?>language" >
<option value="none"><?php echo $PortletText_choisir; ?></option>
<?php

$sSql ="Select portal_languages.id,  portal_languages.lib From portal_languages   ";
      $rs=$GLOBALS["conn"]->Execute($sSql); 
		
 while ( !$rs->EOF)  { 
 	
		  ?>
<option  value="<?php echo $rs->fields["id"]; ?>"><?php echo $rs->fields["lib"]; ?></option>

				  <?php
		  $rs->MoveNext();    
		  }


	
?>
</select><?php formError($portletUID."language")?>

</td>
</tr>


<tr height="20px">
<td colspan="2">

</td>
</tr>
<tr>
<td colspan="2">

<img src="/php/captcha/captcha.php" id="<?php echo $portletUID; ?>captcha" /><br/>



<!-- CHANGE TEXT LINK -->
<a href="#" onclick="
    document.getElementById('<?php echo $portletUID; ?>captcha').src='/php/captcha/captcha.php?'+Math.random();
    document.getElementById('<?php echo $portletUID; ?>captcha-form').focus();"
    id="change-image">Not readable? Change text.</a><br/><br/>


<input type="text" name="<?php echo $portletUID; ?>captcha" id="<?php echo $portletUID; ?>captcha-form" /><?php formError($portletUID."captcha")?><br/>
</td>
</tr>

<tr height="20px">
<td colspan="2">

</td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="Valider" name="<?php echo $portletUID; ?>submit">
</td>
</tr>

</table>
</form>



<?php } 





$error=false;
if (getParameter($portletUID."submit")!=null){
	
/** Validate captcha */
	
if (isset($_REQUEST[$portletUID.'captcha'])) {
	if (trim(strtolower(getParameter($portletUID."captcha"))) != $_SESSION['captcha']) {
				 displayError("Invalid captcha !",$portletUID."captcha");
				// $error=false;
	} 
}
	
if (getParameter($portletUID."portalname")==null) displayError("The portal name is not valid or already used !",$portletUID."portalname");
if (getParameter($portletUID."username")==null) displayError("Please enter a valid username !",$portletUID."username");
if (getParameter($portletUID."password")==null || CheckPassword(getParameter($portletUID."password"))<3) displayError("Please enter a valid password, ".getPasswordScore(CheckPassword(getParameter($portletUID."password")))." !",$portletUID."password");
if (getParameter($portletUID."password")!=getParameter($portletUID."confirmpassword")) displayError("The passwords does not match !",$portletUID."confirmpassword");
if (getParameter($portletUID."mail")==null || ew_CheckEmail(getParameter($portletUID."mail"))==FALSE) displayError("Please enter a valid mail !",$portletUID."mail");
if (getParameter($portletUID."language")==null ||getParameter($portletUID."language") =="none") displayError("Please choose a language for your website !",$portletUID."language");


if ($error==false)
{
	$portalname=getParameter($portletUID."portalname");
	$username=getParameter($portletUID."username");
	$password=getParameter($portletUID."password");
	$mail=getParameter($portletUID."mail");
	$languageId=getParameter($portletUID."language");
	
	//Create image directory
	
	
	
	
	
	

	
	
	
	
	$sSql="INSERT INTO `portal` (`id` ,`name` ,`url` ,`OneColor` ,`TwoColor` ,`ThreeColor` ,`defaultLang` ,`repeatBackground` ,`fontLtId` ,`fontArId` ,`googleAnalytics` ,`addDate` ,`description` ,`keywords`,`isFree`)
			VALUES (NULL , '".$portalname."', '".$portalname."', '#40546A', '#40546A', '#666666', '".$languageId."', 'repeat', '4', '1', 'UA-27481617-1', '".ew_CurrentDateTime()."', NULL , NULL,'Y')";
	//echo $sSql."<br/>"; 
	$GLOBALS["conn"]->Execute($sSql);
	$newPortalId=getLastId();
	
	if ($newPortalId==0) displayError("Unable to create default portal !","portalwizard");
	
	
	
	// add portal app
	$sSql="INSERT INTO `portal_portal_app` (`id` ,`portalId` ,`appId`)
				VALUES (NULL , '".$newPortalId."', '2')";
	//echo $sSql."<br/>";
	$GLOBALS["conn"]->Execute($sSql);
	
	

	$imageId=addUserImage("new-portal-logo.png",$portalname,$newPortalId);
	if ($imageId=="") displayError("Unable to add user image !","portalwizard");
	
	
	$backgroundId=addUserImage("new-portal-background.png",$portalname,$newPortalId);
	if ($backgroundId=="") displayError("Unable to add user image !","portalwizard");
	
	$sSql="UPDATE portal set sitelogo_ar_file=".$imageId.", sitelogo_lt_file=".$imageId.", background_file=".$backgroundId." where id=".$newPortalId;
	
	$GLOBALS["conn"]->Execute($sSql);
	
	
	//Language
	
	$sSql="INSERT INTO `portal_portal_languages` (`id` ,`portalId` ,`languageId`)
			VALUES (NULL , '".$newPortalId."', '".$languageId."')";
	//echo $sSql."<br/>"; 
	$GLOBALS["conn"]->Execute($sSql);
	
	$sSql="SELECT portal_languages.`value` FROM portal_languages WHERE portal_languages.id = ".$languageId;
	
	$rs=$GLOBALS["conn"]->Execute($sSql);
	
	$newlang=$rs->fields["value"];
	
	
	// default Layout
	
	
	$layout='<table id="contentShell" width="100%"  height="100%" cellpadding="0" cellspacing="0">

           <tbody>
    <tr valign="top" height="27px">

                 <td colspan="2"><div id="container4" class="portalBox"></div></td>

              </tr>
 <tr valign="top" height="108px">
<td colspan="2">
<table  width="100%"  cellpadding="0" cellspacing="0">
 <tr valign="top">
<td width="90"><div id="container2" class="portalBox"></div></td>
 <td width="900"><div id="container3" class="portalBox"></div></td>
</tr>
</table>
</td>
                 
 
           
              </tr>
      <tr valign="top">

                 <td colspan="2"><div id="container1" class="portalBox"></div></td>

              </tr>
        

  
           <tr valign="top">

                 <td colspan="2"><div id="container7" class="portalBox"></div></td>

              </tr>

     </tbody>
            

        </table>';
	
	$sSql="INSERT INTO `portal_layout` (`id` ,`portalId` ,`name` ,`layout` ,`icon`)
			VALUES (NULL , '".$newPortalId."', 'Single Column', '".mysql_real_escape_string($layout)."', '1')";
	//echo $sSql."<br/>";
	$GLOBALS["conn"]->Execute($sSql);
	$layoutId=getLastId();
	
	if ($layoutId==0) displayError("Unable to create default Layout !","portalwizard");
	
	// Add Applications
	
	addApplication("portal",$newPortalId);
	addApplication("payment",$newPortalId);
	
	
	
	// Create default page
	
	
	$sSql="INSERT INTO `portal_pages` (`id` ,`pageName` ,`parentId` ,`type` ,`title_FR` ,`title_AR` ,`title_EN` ,`redirectLink` ,`orderCmpnt` ,`portalId` ,`layoutId` ,`layouticon` ,`visibility` ,`active`)
			VALUES (NULL , 'homepage', NULL, 'P', 'Home Page' , NULL , NULL , '0', '1', '".$newPortalId."', '".$layoutId."', '".$layoutId."', 'Y', 'Y')";
	//echo $sSql."<br/>";
	 $GLOBALS["conn"]->Execute($sSql);
	$pageId=getLastId();
	
	// Place portlet in the page
	
	$portletId=7;
	$container="container1";
	
	$porteltPageId=addPortlet($pageId,$portletId,$container);
	
	// Edit preferences of the created portlet
	
	reloadPortletPreferences($porteltPageId);
	
	// Save Preferences
	
	savePortletPreferences($porteltPageId,"testcontent","contentName");
	
	savePortletPreferences($porteltPageId,"testpresentation","presentationTemplate");
	
	
	// Create Authoring Template
	
	$sSql="INSERT INTO `portal_authoringtemplate` (`id` ,`portalId` ,`name` ,`lib` ,`editDate` ,`workflowId`)
			VALUES (NULL , '".$newPortalId."', 'testauthoring', 'Test Authoring', '".ew_CurrentDateTime()."', NULL)";
	//echo $sSql."<br/>";
	 $GLOBALS["conn"]->Execute($sSql);
	$authoringId=getLastId();
	
	
	addAuthoringElement($authoringId,"image","Image");
	addAuthoringElement($authoringId,"date","Date");
	addAuthoringElement($authoringId,"description","TextArea");
	addAuthoringElement($authoringId,"titre","TextField");
	
	// Create Presentation Template
	
	$code='<div style="float:none"><div style="float:left;margin: 0 20px 16px 0;"><img width="204px" src="<display name="image"/>">
</div>
<div style="float:right;width:566px">
<a href="/?pageName=<system name="pageName"/>&content=<system name="contentName"/>&portletId=<prefs name="PortletId"/>&RemotePresentationTemplate=<prefs name="RemotePresentationTemplate"/>"><display name="titre"/></a><br/><br/>
<display name="description"/>
</div></div>
<br class="clear">';
	
	$sSql="INSERT INTO `portal_presentationtemplate` (`id` ,`portalId` ,`name` ,`lib` ,`code` ,`type` ,`editDate`)
			VALUES (NULL , '".$newPortalId."', 'testpresentation', 'Test Presentation', '".mysql_real_escape_string($code)."', 'E', '".ew_CurrentDateTime()."')";
	//echo $sSql."<br/>";
	 $GLOBALS["conn"]->Execute($sSql);
	
	// Create Content
	
	
	$sSql="INSERT INTO `portal_content` (`id` ,`portalId` ,`name` ,`lib` ,`authoringtemplateId` ,`defaultLanguage` ,`localized` ,`date` ,`addDate` ,`editDate` ,`workflowId` ,`workflow_stageId` ,`workflow_statusId`)
			VALUES (NULL , '".$newPortalId."', 'testcontent', 'Test Content', '".$authoringId."', '".$languageId."', 'N', '".ew_CurrentDateTime()."', '".ew_CurrentDateTime()."', '".ew_CurrentDateTime()."', NULL , NULL , '2')";
	//echo $sSql."<br/>";
	 $GLOBALS["conn"]->Execute($sSql);
	$contentId=getLastId();
	// Fill The content
	
	$sSql="Select portal_authoringtemplate_elements.elementId,
  portal_authoringelement.name,
  portal_authoringtemplate_elements.id
From portal_authoringtemplate_elements Inner Join
  portal_authoringelement On portal_authoringelement.id =
    portal_authoringtemplate_elements.elementId
Where portal_authoringtemplate_elements.authoringId =".$authoringId;
	
	$rs=$GLOBALS["conn"]->Execute($sSql);
	
	$currentLang="_".strtoupper($newlang)."_";
	
	while (!$rs->EOF)  {
	
		
		$sSql="INSERT INTO `portal_contentvalues` (`id` ,`contentId` ,`authoringtemplate_elementId` ,`TextField".$currentLang."value` ,`TextArea".$currentLang."value` ,`RichText".$currentLang."value` ,`Datevalue` ,`Imagevalue` ,`InternalLinkvalue` ,`Flashvalue` ,`FlashWidthvalue` ,`FlashHeightvalue` ,`FlashVarsvalue` ,`authoringelementId`)
				VALUES (NULL , '".$contentId."', '".$rs->fields["id"]."',";
		
if ($rs->fields["elementId"]=="1")
{
		$sSql.=" NULL , NULL, NULL , '".ew_CurrentDateTime()."' , NULL , NULL , NULL , NULL , NULL , NULL ,";
}elseif ($rs->fields["elementId"]=="2")
{
	
	$text="This is a test of a text area";
		$sSql.=" NULL , '".$text."', NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL ,";
}elseif ($rs->fields["elementId"]=="3")
{
$text="This is a test of a text field";
		$sSql.=" '".$text."' , NULL, NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL ,";
}elseif ($rs->fields["elementId"]=="8")
{
	
	$sSql.=" NULL , NULL, NULL , NULL , '".$imageId."' , NULL , NULL , NULL , NULL , NULL ,";
}				
				
		
				$sSql.=" '".$rs->fields["elementId"]."');";
		//echo $sSql."<br/>";
		 $GLOBALS["conn"]->Execute($sSql);
		
		$rs->MoveNext();
	}
	
	
	
	
	
	// Create Default User
	
	 
	 addPortalUser($username,$password,$mail,$newPortalId,0,1);
	 
	 
	
	 
	 
	
	// Send Notification Mail
	
	 
	 
	 $msg="Welcome to Butterfly Portal.<br/><br/> A new portal has been created, you cannot log until you activate it.<br/>Please click on the link below to activate your account.<br/><br/>";
	 
	 $key=TEAencrypt($portalname,KEY);
	 $host=".omni-app.com";
	 if (isLocal())  $host=".betasite.net";
	 $link="http://".$portalname.$host."/portal/login.php?operation=activate&key=".$key;
	 $msg.="<a href='".$link."'>".$link."</a><br/><br/>";
	 $msg.="If this methode does not work for you, please copy the link and paste it into your browser.<br/>";
	  sendMailToAdmins($newPortalId,$msg);
	
	  $error=false;
	
	
}
if ($error==false)
{
	echo " Your portal has been succesfully created !.";
	
}


}?>
